<template>
  <div class="home-container">
    <!-- 搜索输入框 -->
    <el-row class="search-row">
      <el-col :span="24">
        <el-input
            v-model="searchQuery"
            placeholder="请输入搜索内容"
            class="search-input"
            style="width: 300px;"
            @input="onSearch"
        >
          <template #append>
            <el-button @click="onSearch">搜索</el-button>
          </template>
        </el-input>
      </el-col>
    </el-row>

    <h1>主页</h1>
    <!-- 第一行：K 线图和饼状图 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="12">
        <div class="chart-container">
          <KLineChart :klineData="klineData" />
        </div>
      </el-col>
      <el-col :span="12">
        <div class="chart-container">
          <PieChart />
        </div>
      </el-col>
    </el-row>

    <!-- 第二行：文本 -->
    <el-row :gutter="20" class="chart-row" style="margin-top: 20px;">
      <el-col :span="24">
        <div class="chart-container">
          <Word />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import KLineChart from './components/K_line.vue';
import PieChart from './components/pie.vue';
import Word from './components/word.vue';
import axios from 'axios';

export default {
  name: 'UserIndex',
  components: {
    KLineChart,
    PieChart,
    Word
  },
  data() {
    return {
      searchQuery: '', // 搜索框的绑定数据
      klineData: [] // 用于存储 K 线图的数据
    };
  },
  methods: {
    async onSearch() {
      // 搜索逻辑
      console.log('Searching for:', this.searchQuery);
      try {
        const response = await axios.get('http://localhost:8000/user/Kline/', {
          params: {
            code: this.searchQuery // 将搜索框的内容作为 code 参数传递给后端
          }
        });
        this.klineData = response.data.data; // 将后端返回的数据存储到 klineData 中
      } catch (error) {
        console.error('Error fetching KLine data:', error);
      }
    }
  }
};
</script>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f9f9f9;
}

.search-row {
  margin-bottom: 20px;
}

h1 {
  margin-bottom: 20px;
  text-align: center;
}

.chart-row {
  margin-bottom: 20px; /* 每行之间的间距 */
}

.chart-container {
  background-color: #fff; /* 白色背景 */
  border-radius: 12px; /* 圆角边框 */
  padding: 20px; /* 容器内边距 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  height: 400px; /* 设置固定高度 */
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
}
</style>